package com.twobu.twobuinsight.security;

/**
 * 用户上下文持有者，用于获取当前登录用户信息
 */
public class UserContextHolder {

    private static final ThreadLocal<UserContext> userContextThreadLocal = new ThreadLocal<>();

    /**
     * 设置当前线程的用户上下文
     */
    public static void setUserContext(UserContext userContext) {
        userContextThreadLocal.set(userContext);
    }

    /**
     * 获取当前线程的用户上下文
     */
    public static UserContext getUserContext() {
        return userContextThreadLocal.get();
    }

    /**
     * 清除当前线程的用户上下文
     */
    public static void clear() {
        userContextThreadLocal.remove();
    }

    /**
     * 获取当前登录用户ID
     */
    public static Long getCurrentUserId() {
        UserContext userContext = getUserContext();
        return userContext != null ? userContext.getUserId() : null;
    }

    /**
     * 获取当前登录用户名
     */
    public static String getCurrentUsername() {
        UserContext userContext = getUserContext();
        return userContext != null ? userContext.getUsername() : null;
    }

    /**
     * 获取当前登录用户租户ID
     */
    public static Integer getCurrentTenantId() {
        UserContext userContext = getUserContext();
        return userContext != null ? userContext.getTenantId() : null;
    }
}
